//
//  WBBBigImgBroswe.m
//  UILbrary
//
//  Created by Apple on 2022/6/6.
//  Copyright © 2022 mac. All rights reserved.
//

#import "WBBBigImgBroswe.h"
@import Masonry;
@import UIKit;
@import SDWebImage;

#define WBB_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define WBB_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height


@interface WBBBigImgCell : UICollectionViewCell<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *scrollViewContentView;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UITapGestureRecognizer *tap;


@end

@implementation WBBBigImgCell

//MARK: - sys

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_configSubviews];
//        [self test];
    }
    return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint tmpPoint = [self convertPoint:point toView:self.imgView];
    BOOL isImgeViewPoint = [self.imgView pointInside:tmpPoint withEvent:event];
    if (isImgeViewPoint) {
        return self.imgView;
    }

    return [super hitTest:point withEvent:event];
}

//MARK: - public

- (void)configWithImgUrl:(NSString *)url
{
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:url]];
}


//MARK: - private

- (void)p_configSubviews
{
    self.maskView.backgroundColor = [UIColor clearColor];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollViewContentView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.maskView];
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.scrollViewContentView];
    [self.scrollViewContentView addSubview:self.imgView];

    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.scrollViewContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.scrollView).insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.width.mas_equalTo(self.scrollView.mas_width);
        make.height.mas_equalTo(self.scrollView.mas_height);
    }];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.scrollViewContentView).insets(UIEdgeInsetsMake(150, 50, 150, 50));
    }];
    
}

//MARK: - action

- (void)tapAction
{
    // 这里主要是拦截 上层的手势事件
//    NSLog(@"dismiss action");
}

//MARK:  - UIScrollViewDelegate

//返回需要缩放的视图控件 缩放过程中
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imgView;
}

//开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
    NSLog(@"开始缩放");
}
//结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"结束缩放");
}

//缩放中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // 延中心点缩放
    CGFloat imageScaleWidth = scrollView.zoomScale * self.imgView.frame.size.width;
    CGFloat imageScaleHeight = scrollView.zoomScale * self.imgView.frame.size.height;
  
    CGFloat imageX = 0;
    CGFloat imageY = 0;
     imageX = floorf((self.frame.size.width - imageScaleWidth) / 2.0);
     imageY = floorf((self.frame.size.height - imageScaleHeight) / 2.0);
     self.imgView.frame = CGRectMake(imageX, imageY, imageScaleWidth, imageScaleHeight);
    
}

//MARK: - get

- (UIView *)maskView
{
    if (!_maskView) {
        _maskView = [UIView new];
    }
    return _maskView;
}


- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIView *)scrollViewContentView
{
    if (!_scrollViewContentView) {
        _scrollViewContentView = [UIView new];
    }
    return _scrollViewContentView;
}

- (UIImageView *)imgView
{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.userInteractionEnabled = YES;
        _imgView.layer.cornerRadius = 15.0;
        _imgView.clipsToBounds = YES;
        [_imgView addGestureRecognizer:self.tap];
    }
    return _imgView;
}

- (UITapGestureRecognizer *)tap
{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    }
    return _tap;
}


//MARK: - test

- (void)test
{
    self.backgroundColor = [UIColor purpleColor];
    self.maskView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.imgView.backgroundColor = [UIColor orangeColor];
}

@end



@interface WBBBigImgBrosweView : UIView<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSArray <NSString *> *imgList;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation WBBBigImgBrosweView


+ (void)showWithImgUrlList:(NSArray <NSString *>*)imgList
{
    [self showWithImgUrlList:imgList index:0];
}

+ (void)showWithImgUrlList:(NSArray <NSString *>*)imgList index:(NSUInteger)index
{
    if(index >= imgList.count) return;
    
    WBBBigImgBrosweView *view = [WBBBigImgBrosweView new];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:view];
    view.frame = window.bounds;
    
    view.imgList = imgList;
    [view p_configSubviews];
    [view showIndex:index];
}

//MARK: - system

- (void)dealloc
{
    NSLog(@"%s",__func__);
}

- (instancetype)init
{
    if (self = [super init]) {
        _imgList = @[];
        self.backgroundColor = [UIColor clearColor];
        self.collectionView.backgroundColor = [UIColor clearColor];
        [self addGestureRecognizer:self.tap];

        [self test];
    }
    return self;
}

//MARK: - public

- (void)showIndex:(NSUInteger)index
{
    dispatch_async(dispatch_get_main_queue(), ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0];
            [self.collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        });
    });
}

//MARK: - private

- (void)p_configSubviews
{
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5];
    [self addSubview:self.collectionView];
    [self addSubview:self.pageControl];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom).offset(-100);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.width.mas_equalTo(200);
        make.height.mas_offset(50);
    }];
}

- (void)p_dismiss
{
    [self removeFromSuperview];
}

//MARK: - UICollectionViewDataSource UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imgList.count;
}


-(CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0,0);
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WBBBigImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WBBBigImgCell" forIndexPath:indexPath];
    if(indexPath.row < self.imgList.count){
        [cell configWithImgUrl:self.imgList[indexPath.row]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
//    [self.delegate JXRescueBrandView:self clickIndex:indexPath];
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.pageControl.currentPage = indexPath.row;
}


//MARK: - action

- (void)tapAction
{
    NSLog(@"%s",__func__);
    [self p_dismiss];
}

//MARK: - get

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        [_collectionView registerClass:[WBBBigImgCell class] forCellWithReuseIdentifier:@"WBBBigImgCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout
{
    if (!_layout) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(0, 0 ,0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.itemSize = CGSizeMake(WBB_SCREEN_WIDTH, WBB_SCREEN_HEIGHT);
//        layout.estimatedItemSize = CGSizeMake(itemSize, itemSize);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout = layout;
    }
    return _layout;
}

- (UITapGestureRecognizer *)tap
{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    }
    return _tap;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.numberOfPages = self.imgList.count;
        _pageControl.currentPage = 0;
        _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    }
    return _pageControl;
}

//MARK: - test

- (void)test
{
//    self.backgroundColor = [UIColor grayColor];
//    self.collectionView.backgroundColor = [UIColor greenColor];
}

@end





@implementation WBBBigImgBroswe

+ (void)showWithImgUrlList:(NSArray <NSString *>*)imgList
{
    [WBBBigImgBrosweView showWithImgUrlList:imgList];
}

+ (void)showWithImgUrlList:(NSArray <NSString *>*)imgList index:(NSUInteger)index
{
    [WBBBigImgBrosweView showWithImgUrlList:imgList index:index];
}


@end
